# -*- coding: utf-8 -*-
"""
-------------------------------------------------
  Description :
  Author :    Administrator
  date：     2021/10/10
-------------------------------------------------
  Change Activity:
          2021/10/10:
-------------------------------------------------
"""
import re
import ddddocr
from Config.config import logger
ocr_engine = ddddocr.DdddOcr()


def ocr_img(img_path):
    """
    保存验证码图片到本地，进行识别
    :return  识别成功返回文本，失败返回False
    """
    # 识别验证码
    try:
        with open(img_path, 'rb') as r:
            img_bytes = r.read()

        text = ocr_engine.classification(img_bytes)
        if re.search(r'[0-9a-zA-Z]{4}', text) is not None and len(text) == 4:
            return text
        else:
            return False
    except Exception as e:
        logger.warning(f'ocr 识别失败 > {e}')
        return False


if __name__ == '__main__':
    ocr_img(img_path=r'F:\Other\LibraryRegister\3.jpg')
